package com.example.demo.dao.daoimpl;

import com.example.demo.dao.AccountMapper;
import com.example.demo.pojo.Account;
import com.example.demo.pojo.Page;
import com.example.demo.vo.AccountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.util.List;

/**
 * <h3>reqister</h3>
 * <p>User表操作类</p>
 *
 * @author : chenjunjie
 **/
@Repository
public class AccountMapperImpl implements AccountMapper {

    @Autowired
    DataSource dataSource;

    @Autowired
    JdbcTemplate template;
    @Autowired
    NamedParameterJdbcTemplate namedTemplate;

    @Override
    public Account findByName(String username){
        Account account = template.queryForObject("select account_name,account_password,status,role_id,account_realname,account_id" +
                        " from account where account_name =?", new Account(), username);
        return account;
    }


    @Override
    public List<Account> findAccountList(Page page,String username,String realname) {
        List<Account> accounts=null;
        StringBuffer sql=new StringBuffer("select account_name,account_password,role_id,status,account_realname,account_id from account");
        if("".equals(username)&&"".equals(realname)){
            sql.append(" limit ?,?");
            accounts = template.query(sql.toString(),
                    new Account(),(page.getCurrentPage()-1)*page.getPageSize(),page.getCurrentPage()*page.getPageSize()-1);
        }else{
            sql.append(" where account_name=? or account_realname = ?  limit ?,?");
            accounts = template.query(sql.toString(),
                    new Account(),username,realname,(page.getCurrentPage()-1)*page.getPageSize(),page.getCurrentPage()*page.getPageSize()-1);
        }

        return accounts;
    }

    @Override
    public Integer getCount() {
        Integer totalCount = template.queryForObject("select count(id) from account", Integer.class);
        return totalCount;
    }

    /**
     * TODO 没有动态判空
     * @param account
     * @return
     */
    @Override
    public boolean updateAccount(AccountVo account) {
        SqlParameterSource parameterSource=new BeanPropertySqlParameterSource(account);
        int update = namedTemplate.update("update account set account_name=:username,account_password=:password,role_id=:roleId,status=:status,account_realname=:realName" +
                " where account_id=:id", parameterSource);
        return update>0?true:false;
    }

    @Override
    public boolean deleteAccount(String accountId) {
        int update = template.update("delete from account where account_id = ?", accountId);
        return update>0?true:false;
    }

    @Override
    public boolean updateStatus(String accountId,Integer status) {
        int update = template.update("update account set status=? where account_id = ?", status, accountId);
        return update>0?true:false;
    }

    @Override
    public boolean insertAccount(AccountVo accountVo) {
        SqlParameterSource parameterSource=new BeanPropertySqlParameterSource(accountVo);
        int update = namedTemplate.update("insert into account (account_name,account_password,status,role_id,account_realname,account_id)" +
                "values(:username,:password,:status,:roleId,:realName,REPLACE(UUID(),'-',''))",parameterSource);
        return update>0?true:false;
    }
}
